{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import xgboost as xgb\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from imblearn.under_sampling import RandomUnderSampler\n",
    "from imblearn.over_sampling import SMOTE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取数据\n",
    "import numpy as np \n",
    "trainData_path = r'./data/trainStand.xlsx'\n",
    "trainData = pd.read_excel(trainData_path, engine='openpyxl')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_indicator = ['novelty', 'number_of_patents_cited', 'science_linkage',\n",
    "       'number_of_IPCs', 'number_of_claims', 'number_of_independent_claims',\n",
    "       'number_of_dependent_claims', 'number_of_priorities', 'review_duration',\n",
    "       'number_of_patent_families', 'number_of_countries_applying',\n",
    "       'number_of_inventors', 'number_of_patentees',\n",
    "       'number_of_inventor_patents', 'number_of_first_inventor_patents',\n",
    "       'number_of_patentee_patents', 'patentee_category', 'label']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "reduce_indicator = ['number_of_patents_cited', 'science_linkage',\n",
    "\t\t'number_of_priorities', 'review_duration',\n",
    "    \t'number_of_countries_applying',\n",
    "    \t'number_of_patentees', 'number_of_patentee_patents', 'patentee_category',\n",
    "    \t'number_of_inventor_patents', 'number_of_first_inventor_patents',\n",
    "        'label']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 约减后指标数据(训练集与验证集)\n",
    "trainDataReduce=trainData.loc[:,reduce_indicator]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "col_num = trainDataReduce.shape[1] - 1\n",
    "colSelect = col_num \n",
    "x = trainDataReduce.iloc[:,range(colSelect)].values\n",
    "y = trainDataReduce.iloc[:,col_num].values\n",
    "\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "x_train,x_valid,y_train,y_valid = train_test_split(x,y,test_size=0.2,stratify=y,random_state=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 过采样\n",
    "smote = SMOTE()\n",
    "x_train_resampled,y_train_resampled = smote.fit_resample(x_train,y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 构建模型\n",
    "tabModel = xgb.XGBClassifier(max_depth=6, \n",
    "\t\t\t\tlearning_rate=0.1, \n",
    "\t\t\t\tn_estimators=100, \n",
    "\t\t\t\tsilent=True, \n",
    "\t\t\t\tobjective='binary:logistic',\n",
    "\t\t\t\tscale_pos_weight=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Python36\\lib\\site-packages\\xgboost\\sklearn.py:1146: UserWarning: The use of label encoder in XGBClassifier is deprecated and will be removed in a future release. To remove this warning, do the following: 1) Pass option use_label_encoder=False when constructing XGBClassifier object; and 2) Encode your labels (y) as integers starting with 0, i.e. 0, 1, 2, ..., [num_class - 1].\n",
      "  warnings.warn(label_encoder_deprecation_msg, UserWarning)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[11:43:42] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.4.0/src/learner.cc:573: \n",
      "Parameters: { \"silent\" } might not be used.\n",
      "\n",
      "  This may not be accurate due to some parameters are only used in language bindings but\n",
      "  passed down to XGBoost core.  Or some parameters are not used but slip through this\n",
      "  verification. Please open an issue if you find above cases.\n",
      "\n",
      "\n",
      "[11:43:42] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.4.0/src/learner.cc:1095: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,\n",
       "              colsample_bynode=1, colsample_bytree=1, gamma=0, gpu_id=-1,\n",
       "              importance_type='gain', interaction_constraints='',\n",
       "              learning_rate=0.1, max_delta_step=0, max_depth=6,\n",
       "              min_child_weight=1, missing=nan, monotone_constraints='()',\n",
       "              n_estimators=100, n_jobs=8, num_parallel_tree=1, random_state=0,\n",
       "              reg_alpha=0, reg_lambda=1, scale_pos_weight=1, silent=True,\n",
       "              subsample=1, tree_method='exact', validate_parameters=1,\n",
       "              verbosity=None)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 训练模型\n",
    "tabModel.fit(x_train_resampled,y_train_resampled)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train accuracy score: 0.8088733216579101\n",
      "Confusion matrix:\n",
      "[[3361  734]\n",
      " [ 903 3567]]\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.79      0.82      0.80      4095\n",
      "           1       0.83      0.80      0.81      4470\n",
      "\n",
      "    accuracy                           0.81      8565\n",
      "   macro avg       0.81      0.81      0.81      8565\n",
      "weighted avg       0.81      0.81      0.81      8565\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import confusion_matrix,classification_report,accuracy_score\n",
    "# 训练集评估\n",
    "y_train_predict = tabModel.predict(x_train)\n",
    "\n",
    "train_acc = accuracy_score(y_train,y_train_predict)\n",
    "print('train accuracy score:',train_acc)\n",
    "\n",
    "confuseMetrix_train = confusion_matrix(y_train,y_train_predict)\n",
    "print(\"Confusion matrix:\\n{}\".format(confuseMetrix_train))\n",
    "\n",
    "classificationReport_train = classification_report(y_train,y_train_predict,digits=4)\n",
    "print(classificationReport_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "valid accuracy score: 0.7371615312791784\n",
      "Confusion matrix:\n",
      "[[754 270]\n",
      " [293 825]]\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.72      0.74      0.73      1024\n",
      "           1       0.75      0.74      0.75      1118\n",
      "\n",
      "    accuracy                           0.74      2142\n",
      "   macro avg       0.74      0.74      0.74      2142\n",
      "weighted avg       0.74      0.74      0.74      2142\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#验证集评估\n",
    "y_valid_predict = tabModel.predict(x_valid)\n",
    "\n",
    "valid_acc = accuracy_score(y_valid,y_valid_predict)\n",
    "print('valid accuracy score:',valid_acc)\n",
    "\n",
    "confuseMetrix_valid = confusion_matrix(y_valid,y_valid_predict)\n",
    "print(\"Confusion matrix:\\n{}\".format(confuseMetrix_valid))\n",
    "\n",
    "classificationReport_valid = classification_report(y_valid,y_valid_predict,digits=4)\n",
    "print(classificationReport_valid)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "#测试集评估\n",
    "testData_path = r'./data/testStand.xlsx'\n",
    "testData = pd.read_excel(testData_path, engine='openpyxl')\n",
    "testDataReduce = testData.loc[:,reduce_indicator]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "col_num = testDataReduce.shape[1] - 1\n",
    "colSelect = col_num \n",
    "x_test = testDataReduce.iloc[:,range(colSelect)].values\n",
    "y_test = testDataReduce.iloc[:,col_num].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "test accuracy score: 0.7235852142623487\n",
      "Confusion matrix:\n",
      "[[1502  388]\n",
      " [ 457  710]]\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.77      0.79      0.78      1890\n",
      "           1       0.65      0.61      0.63      1167\n",
      "\n",
      "    accuracy                           0.72      3057\n",
      "   macro avg       0.71      0.70      0.70      3057\n",
      "weighted avg       0.72      0.72      0.72      3057\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 使用测试集评估模型的泛化能力\n",
    "y_test_predict = tabModel.predict(x_test)\n",
    "\n",
    "test_acc = accuracy_score(y_test,y_test_predict)\n",
    "print('test accuracy score:',test_acc)\n",
    "\n",
    "confuseMetrix_test = confusion_matrix(y_test,y_test_predict)\n",
    "print(\"Confusion matrix:\\n{}\".format(confuseMetrix_test))\n",
    "\n",
    "classificationReport_test = classification_report(y_test,y_test_predict,digits=4)\n",
    "print(classificationReport_test)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.8 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "d207591e6ff77a7c5fb4cef0dd9fd3703274637a9d0902d2045beb3a65bf572a"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
